//
//  CoreMessageDisplayView.m
//  mobile
//
//  Created by abc on 2018/4/3.
//  Copyright © 2018年 azazie. All rights reserved.
//

#import "CoreMessageDisplayView.h"

#import "TXFire.h"
#import <QMUIKit/QMUIKit.h>
#import <Masonry/Masonry.h>

@interface CoreMessageDisplayView ()
{
    CGFloat _sideSpace;
    CGFloat _topSpace;
    CGFloat _bottomSpace;
    CGFloat _stripGap;
}
@property (nonatomic, strong) UIView *titleView;
@property (nonatomic, strong) UIView *messageView;
@property (nonatomic, strong) UIView *changeView;

@property (nonatomic, assign) BOOL changeable;
@property (nonatomic, strong) NSArray <NSString *> *messageArray;
@end

@implementation CoreMessageDisplayView


- (instancetype)init {
    if (self = [super init]) {
        self = self.commonInit.configureSubviews.makeConstraints.subscribe;
        
        self.changeable = YES;
    }
    return self;
}

- (instancetype)commonInit {
    {
        _sideSpace = 15.f;
        _topSpace = 20.f;
        _bottomSpace = 20.f;
        _stripGap = 9.f;
    }
    self.backgroundColor = UIColorWhite;
    return self;
}

- (instancetype)configureSubviews {
    [self addSubview:self.changeView];
    [self addSubview:self.titleView];
    [self addSubview:self.messageView];
    return self;
}

- (instancetype)makeConstraints {
    [self.changeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(_topSpace);
        make.right.equalTo(self).offset(-_sideSpace);
    }];
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.changeView);
        make.left.equalTo(self).offset(_sideSpace);
        make.right.equalTo(self.changeView.mas_left).offset(-_sideSpace);
    }];
    [self.messageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleView.mas_bottom);
        make.left.right.equalTo(self.titleView);
        make.bottom.equalTo(self).offset(-_bottomSpace);
    }];
    return self;
}

- (instancetype)subscribe {
    @weakify(self);
    [[RACObserve(self, messageArray) distinctUntilChanged] subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self refreshMessageArea];
    }];
    [[RACObserve(self, changeable) distinctUntilChanged] subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self.changeView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        if (self.changeable) {
            UILabel *changeLabel = [self changeLabel];
            [self.changeView addSubview:changeLabel];
            [changeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(self.changeView);
                make.width.mas_offset([self labelWidthForString:@"Change"]);
            }];
        } else {
            UIView *paddingView = [[UIView alloc] init];
            [self.changeView addSubview:paddingView];
            [paddingView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(self.changeView);
                make.size.mas_equalTo(CGSizeMake(1, 1));
            }];
        }
    }];
    
    self.signalForChange = [self.changeView.tx_tapGestureRecognizer rac_gestureSignal];
    return self;
}

- (void)refreshMessageArea {
    [self.messageView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    if (!self.messageArray) {
        UILabel *label = [self messageLabelForText:@"there no data"];
        [self.messageView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.left.right.equalTo(self);
            make.top.equalTo(self);
            make.bottom.equalTo(self.mas_bottom);
            make.height.mas_equalTo([self labelHeightForString:@"there no data"]);
        }];
        return ;
    }
    
    UILabel *previousLabel;
    for (int i=0; i<self.messageArray.count; i++) {
        UILabel *label = [self messageLabelForText:self.messageArray[i]];
        
        [self.messageView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.left.right.equalTo(self.messageView);
            if (i == 0) {
                make.top.equalTo(self.messageView);
            } else {
                make.top.equalTo(previousLabel.mas_bottom).offset(_stripGap);
            }
            if (i == self.messageArray.count-1) {
                make.bottom.equalTo(self.messageView.mas_bottom);
            }
            make.height.mas_equalTo([self labelHeightForString:self.messageArray[i]]);
        }];
        previousLabel = label;
    }
}

- (CGFloat)labelHeightForString:(NSString *)value {
    CGRect infoRect = [value boundingRectWithSize:CGSizeMake(self.messageView.bounds.size.width, 1000)
                                          options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                       attributes:@{NSFontAttributeName : UIFontMake(13)}
                                          context:nil];
    NSUInteger lineNumber = infoRect.size.height / (UIFontMake(13).lineHeight);
    return lineNumber * 13.f;
}

- (CGFloat)labelWidthForString:(NSString *)value {
    return [@"Change" sizeWithAttributes:@{NSFontAttributeName:UIFontMake(13)}].width + 1;
}

#pragma mark - customization functions
//refersh paddings
- (CoreMessageDisplayView *(^)(BOOL changeable))changeAble {
    return ^(BOOL changeable) {
        self.changeable = changeable;
        return self;
    };
}

- (CoreMessageDisplayView *(^)(CGFloat space))sidePadding {
    return ^(CGFloat space) {
        _sideSpace = space;
        [self.changeView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-_sideSpace);
        }];
        [self.titleView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(_sideSpace);
            make.right.equalTo(self.changeView.mas_left).offset(-_sideSpace);
        }];
        return self;
    };
}

- (CoreMessageDisplayView *(^)(CGFloat space))topPadding {
    return ^(CGFloat space) {
        _topSpace = space;
        [self.changeView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(_topSpace);
        }];
        return self;
    };
}

- (CoreMessageDisplayView *(^)(CGFloat space))bottomPadding {
    return ^(CGFloat space) {
        _bottomSpace = space;
        [self.messageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self).offset(-_bottomSpace);
        }];
        return self;
    };
}

- (CoreMessageDisplayView *(^)(CGFloat space))stripPadding {
    return ^(CGFloat space) {
        _stripGap = space;
        [self refreshMessageArea];
        return self;
    };
}

- (CoreMessageDisplayView *(^)(NSArray <NSString *> *messages))messages {
    return ^(NSArray <NSString *> *messages) {
        self.messageArray = messages;
        return self;
    };
}

- (CoreMessageDisplayView *(^)(NSString *title))title {
    return ^(NSString *title) {
        [self.titleView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        if (title.length > 0) {
            UILabel *titleLabel = [self titleLabelForText:title];
            [self.titleView addSubview:titleLabel];
            [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.left.equalTo(self.titleView);
                make.bottom.equalTo(self.titleView).offset(-20.f);
            }];
        }
        return self;
    };
}

#pragma mark - lazy load for
- (UIView *)titleView {
    if (!_titleView) {
        _titleView = [[UIView alloc] init];
    }
    return _titleView;
}

- (UIView *)messageView {
    if (!_messageView) {
        _messageView = [[UIView alloc] init];
    }
    return _messageView;
}

- (UIView *)changeView {
    if (!_changeView) {
        _changeView = [[UIView alloc] init];
    }
    return _changeView;
}

- (UILabel *)changeLabel {
    UILabel *changeLabel = [[UILabel alloc] qmui_initWithFont:UIFontMake(13.f) textColor:[UIColor tx_colorWithHex:0xe2a3aa]];
    changeLabel.text = @"Change";
    return changeLabel;
}

- (UILabel *)titleLabelForText:(NSString *)title {
    UILabel *titleLabel = [[UILabel alloc] qmui_initWithFont:UIFontBoldMake(12) textColor:[UIColor tx_colorWithHex:0x333333]];
    titleLabel.text = title;
    return titleLabel;
}

- (UILabel *)messageLabelForText:(NSString *)message {
    UILabel *label = [[UILabel alloc] qmui_initWithFont:UIFontMake(13) textColor:[UIColor tx_colorWithHex:0x333333]];
    
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByWordWrapping;

    label.text = message;
    return label;
}

@end
